home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Timestamp.java < prev    next >
Text File  |  1998-09-22  |  9KB  |  317 lines

  1. /*
  2.  * @(#)Timestamp.java    1.13 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.sql;
  16.  
  17. /**
  18.  * <P>This class is a thin wrapper around java.util.Date that allows
  19.  * JDBC to identify this as a SQL TIMESTAMP value. It adds the ability
  20.  * to hold the SQL TIMESTAMP nanos value and provides formatting and
  21.  * parsing operations to support the JDBC escape syntax for timestamp
  22.  * values.
  23.  *
  24.  * <P><B>Note:</B> This type is a composite of a java.util.Date and a
  25.  * separate nanos value. Only integral seconds are stored in the
  26.  * java.util.Date component. The fractional seconds - the nanos - are
  27.  * separate. The getTime method will only return integral seconds. If
  28.  * a time value that includes the fractional seconds is desired you
  29.  * must convert nanos to milliseconds (nanos/1000000) and add this to
  30.  * the getTime value. Also note that the hashcode() method uses the
  31.  * underlying java.util.Data implementation and therefore does not
  32.  * include nanos in its computation.  
  33.  */
  34. public class Timestamp extends java.util.Date {
  35.  
  36.     /**
  37.      * Construct a Timestamp Object
  38.      *
  39.      * @param year year-1900
  40.      * @param month 0 to 11 
  41.      * @param day 1 to 31
  42.      * @param hour 0 to 23
  43.      * @param minute 0 to 59
  44.      * @param second 0 to 59
  45.      * @param nano 0 to 999,999,999
  46.      */
  47.     public Timestamp(int year, int month, int date, 
  48.              int hour, int minute, int second, int nano) {
  49.     super(year, month, date, hour, minute, second);
  50.     if (nano > 999999999 || nano < 0) {
  51.         throw new IllegalArgumentException("nanos > 999999999 or < 0");
  52.     }
  53.     nanos = nano;
  54.     }
  55.  
  56.     /**
  57.      * Construct a Timestamp using a milliseconds time value. The
  58.      * integral seconds are stored in the underlying date value; the
  59.      * fractional seconds are stored in the nanos value.
  60.      *
  61.      * @param time milliseconds since January 1, 1970, 00:00:00 GMT 
  62.      */
  63.     public Timestamp(long time) {
  64.     super((time/1000)*1000);
  65.     nanos = (int)((time%1000) * 1000000);
  66.     if (nanos < 0) {
  67.         nanos = 1000000000 + nanos;        
  68.         setTime(((time/1000)-1)*1000);
  69.     }
  70.     }
  71.  
  72.     private int nanos;
  73.  
  74.     /**
  75.      * Convert a string in JDBC timestamp escape format to a Timestamp value
  76.      *
  77.      * @param s timestamp in format "yyyy-mm-dd hh:mm:ss.fffffffff"
  78.      * @return corresponding Timestamp
  79.      */
  80.     public static Timestamp valueOf(String s) {
  81.     String date_s;
  82.     String time_s;
  83.     String nanos_s;
  84.     int year;
  85.     int month;
  86.     int day;
  87.     int hour;
  88.     int minute;
  89.     int second;
  90.     int a_nanos = 0;
  91.     int firstDash;
  92.     int secondDash;
  93.     int dividingSpace;
  94.     int firstColon = 0;
  95.     int secondColon = 0;
  96.     int period = 0;
  97.     String formatError = "Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff";
  98.     String zeros = "000000000";
  99.  
  100.     if (s == null) throw new java.lang.IllegalArgumentException("null string");
  101.  
  102.     // Split the string into date and time components
  103.     s = s.trim();
  104.     dividingSpace = s.indexOf(' ');
  105.     if (dividingSpace > 0) {
  106.         date_s = s.substring(0,dividingSpace);
  107.         time_s = s.substring(dividingSpace+1);
  108.     } else {
  109.         throw new java.lang.IllegalArgumentException(formatError);
  110.     }
  111.  
  112.  
  113.     // Parse the date
  114.     firstDash = date_s.indexOf('-');
  115.     secondDash = date_s.indexOf('-', firstDash+1);
  116.  
  117.     // Parse the time
  118.     if (time_s == null) 
  119.         throw new java.lang.IllegalArgumentException(formatError);
  120.     firstColon = time_s.indexOf(':');
  121.     secondColon = time_s.indexOf(':', firstColon+1);
  122.     period = time_s.indexOf('.', secondColon+1);
  123.  
  124.     // Convert the date
  125.     if ((firstDash > 0) & (secondDash > 0) & 
  126.         (secondDash < date_s.length()-1)) {
  127.         year = Integer.parseInt(date_s.substring(0, firstDash)) - 1900;
  128.         month = 
  129.         Integer.parseInt(date_s.substring
  130.                  (firstDash+1, secondDash)) - 1;
  131.         day = Integer.parseInt(date_s.substring(secondDash+1));
  132.     } else {        
  133.         throw new java.lang.IllegalArgumentException(formatError);
  134.     }
  135.  
  136.     // Convert the time; default missing nanos
  137.     if ((firstColon > 0) & (secondColon > 0) & 
  138.         (secondColon < time_s.length()-1)) {
  139.         hour = Integer.parseInt(time_s.substring(0, firstColon));
  140.         minute = 
  141.         Integer.parseInt(time_s.substring(firstColon+1, secondColon));
  142.         if ((period > 0) & (period < time_s.length()-1)) {
  143.         second = 
  144.             Integer.parseInt(time_s.substring(secondColon+1, period));
  145.         nanos_s = time_s.substring(period+1);
  146.         if (nanos_s.length() > 9) 
  147.             throw new java.lang.IllegalArgumentException(formatError);
  148.         if (!Character.isDigit(nanos_s.charAt(0)))
  149.             throw new java.lang.IllegalArgumentException(formatError);
  150.         nanos_s = nanos_s + zeros.substring(0,9-nanos_s.length());
  151.         a_nanos = Integer.parseInt(nanos_s);
  152.         } else if (period > 0) {
  153.         throw new java.lang.IllegalArgumentException(formatError);
  154.         } else {
  155.         second = Integer.parseInt(time_s.substring(secondColon+1));
  156.         }
  157.     } else {
  158.         throw new java.lang.IllegalArgumentException();
  159.     }
  160.  
  161.     return new Timestamp(year, month, day, hour, minute, second, a_nanos);
  162.     }
  163.  
  164.     /**
  165.      * Format a timestamp in JDBC timestamp escape format
  166.      *
  167.      * @return a String in yyyy-mm-dd hh:mm:ss.fffffffff format
  168.      */
  169.     public String toString () {
  170.     int year = super.getYear() + 1900;
  171.     int month = super.getMonth() + 1;
  172.     int day = super.getDate();
  173.     int hour = super.getHours();
  174.     int minute = super.getMinutes();
  175.     int second = super.getSeconds();
  176.     String yearString;
  177.     String monthString;
  178.     String dayString;
  179.     String hourString;
  180.     String minuteString;
  181.     String secondString;
  182.     String nanosString;
  183.     String zeros = "000000000";
  184.  
  185.         
  186.     yearString = "" + year;
  187.     if (month < 10) {
  188.         monthString = "0" + month;
  189.     } else {        
  190.         monthString = Integer.toString(month);
  191.     }
  192.     if (day < 10) {
  193.         dayString = "0" + day;
  194.     } else {        
  195.         dayString = Integer.toString(day);
  196.     }
  197.     if (hour < 10) {
  198.         hourString = "0" + hour;
  199.     } else {        
  200.         hourString = Integer.toString(hour);
  201.     }
  202.     if (minute < 10) {
  203.         minuteString = "0" + minute;
  204.     } else {        
  205.         minuteString = Integer.toString(minute);
  206.     }
  207.     if (second < 10) {
  208.         secondString = "0" + second;
  209.     } else {        
  210.         secondString = Integer.toString(second);
  211.     }
  212.     if (nanos == 0) {
  213.         nanosString = "0";
  214.     } else {
  215.         nanosString = Integer.toString(nanos);
  216.  
  217.         // Add leading zeros
  218.         nanosString = zeros.substring(0,(9-nanosString.length())) + 
  219.         nanosString;
  220.         
  221.         // Truncate trailing zeros
  222.         char[] nanosChar = new char[nanosString.length()];
  223.         nanosString.getChars(0, nanosString.length(), nanosChar, 0);
  224.         int truncIndex = 8;        
  225.         while (nanosChar[truncIndex] == '0') {
  226.         truncIndex--;
  227.         }
  228.         nanosString = new String(nanosChar,0,truncIndex+1);
  229.     }
  230.     
  231.     return (yearString + "-" + monthString + "-" + dayString + " " + 
  232.         hourString + ":" + minuteString + ":" + secondString + "."
  233.                 + nanosString);
  234.     }
  235.  
  236.     /**
  237.      * Get the Timestamp's nanos value
  238.      *
  239.      * @return the Timestamp's fractional seconds component
  240.      */
  241.     public int getNanos() {
  242.     return nanos;
  243.     }
  244.  
  245.     /**
  246.      * Set the Timestamp's nanos value
  247.      *
  248.      * @param n the new fractional seconds component
  249.      */
  250.     public void setNanos(int n) {
  251.     if (n > 999999999 || n < 0) {
  252.         throw new IllegalArgumentException("nanos > 999999999 or < 0");
  253.     }
  254.     nanos = n;
  255.     }
  256.  
  257.     /**
  258.      * Test Timestamp values for equality
  259.      *
  260.      * @param ts the Timestamp value to compare with
  261.      */
  262.     public boolean equals(Timestamp ts) {
  263.     if (super.equals(ts)) {
  264.         if (nanos == ts.nanos) {
  265.         return true;
  266.         } else {
  267.         return false;
  268.         }
  269.     } else {
  270.         return false;
  271.     }
  272.     }
  273.  
  274.     /**
  275.      * Is this timestamp earlier than the timestamp argument?
  276.      *
  277.      * @param ts the Timestamp value to compare with
  278.      */
  279.     public boolean before(Timestamp ts) {
  280.     if (super.before(ts)) {
  281.         return true;
  282.     } else {
  283.         if (super.equals(ts)) {
  284.         if (nanos < ts.nanos) {
  285.             return true;
  286.         } else {
  287.             return false;
  288.         }
  289.         } else {
  290.         return false;
  291.         }
  292.     }
  293.     }
  294.  
  295.     /**
  296.      * Is this timestamp later than the timestamp argument?
  297.      *
  298.      * @param ts the Timestamp value to compare with
  299.      */
  300.     public boolean after(Timestamp ts) {
  301.     if (super.after(ts)) {
  302.         return true;
  303.     } else {
  304.         if (super.equals(ts)) {
  305.         if (nanos > ts.nanos) {
  306.             return true;
  307.         } else {
  308.             return false;
  309.         }
  310.         } else {
  311.         return false;
  312.         }
  313.     }
  314.     }
  315. }
  316.  
  317.